From 03a7f0504a5427918aa2cb37e3e9064bae24803e Mon Sep 17 00:00:00 2001 From: Evgen Druzhynin Date: Wed, 21 Jun 2017 19:48:40 +0300 Subject: [PATCH] Fix an incorrect merge of credentials. Add a new test for that. --- src/cargo/util/config.rs | 19 ++++++++++++------- tests/login.rs | 16 ++++++++++++++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/cargo/util/config.rs b/src/cargo/util/config.rs index 5edeeef96..2df174740 100644 --- a/src/cargo/util/config.rs +++ b/src/cargo/util/config.rs @@ -486,13 +486,18 @@ impl Config { _ => unreachable!(), }; - let mut registry = cfg.entry("registry".into()) - .or_insert(CV::Table(HashMap::new(), - PathBuf::from("."))); - registry.merge(value).chain_err(|| { - format!("failed to merge configuration at `{}`", - credentials.display()) - })?; + let registry = cfg.entry("registry".into()) + .or_insert(CV::Table(HashMap::new(), PathBuf::from("."))); + + match (registry, value) { + (&mut CV::Table(ref mut old, _), CV::Table(ref mut new, _)) => { + let new = mem::replace(new, HashMap::new()); + for (key, value) in new.into_iter() { + old.insert(key, value); + } + } + _ => unreachable!(), + } Ok(()) } diff --git a/tests/login.rs b/tests/login.rs index 306b4e0d9..d3b9b601b 100644 --- a/tests/login.rs +++ b/tests/login.rs @@ -11,6 +11,8 @@ use cargotest::cargo_process; use cargotest::support::execs; use cargotest::support::registry::registry; use cargotest::install::cargo_home; +use cargo::util::config::Config; +use cargo::core::Shell; use hamcrest::{assert_that, existing_file, is_not}; const TOKEN: &str = "test-token"; @@ -110,3 +112,17 @@ fn login_without_credentials() { File::open(&credentials).unwrap().read_to_string(&mut contents).unwrap(); assert!(check_host_token(contents.parse().unwrap())); } + +#[test] +fn new_credentials_is_used_instead_old() { + setup_old_credentials(); + setup_new_credentials(); + + assert_that(cargo_process().arg("login") + .arg("--host").arg(registry().to_string()).arg(TOKEN), + execs().with_status(0)); + + let config = Config::new(Shell::new(), cargo_home(), cargo_home()); + let token = config.get_string("registry.token").unwrap().map(|p| p.val); + assert!(token.unwrap() == TOKEN); +} -- 2.30.2